class TreeNode(object):
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right


class Solution(object):
    def isUnivalTree(self, root):
        """
        :type root: TreeNode
        :rtype: bool
        """
        stack = [root]
        val = root.val
        while stack:
            node = stack[-1]
            if val != node.val:
                return False
            if node.left:
                stack.append(node.left)
            else:
                node = stack.pop()
                while stack and not node.right:
                    node = stack.pop()
                if node.right:
                    stack.append(node.right)
        return True

